Java Keywords by Worasait Suwannik

Java Keywords by Worasait Suwannik

Author:Worasait Suwannik [Suwannik, Worasait]
Language: eng
Format: mobi
Publisher: Wannik Publishing
Published: 2011-03-24T16:00:00+00:00


public class FixIt {

public static void main(String[] args) {

int a = 1;

int b = 2;

System.out.println(a / b);

}

}

However, it prints:

0

The reason is because when both operands are integer, Java performs an integer division. To force Java to perform a floating point division, we cast one operand with double.

public class FixIt {

public static void main(String[] args) {

int a = 1;

int b = 2;

System.out.println((double) a / b);

}

}

The result is:

0.5

Fix It

The following program should print the result of 123 plus 12.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.